package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Employee;
import cn.wolfcode.quest.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//修改密码控制器
@Controller
public class UpdatePwdController {
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/updatePwd")
    public String updatePwd(){
        return "common/updatePwd";
    }
    @RequestMapping("/checkPwd")
    @ResponseBody
    public JsonResult checkPwd(String oldPassword,String newPassword){
        System.out.println(oldPassword);
        JsonResult jsonResult = new JsonResult();
        //判断新老密码是否为空
        if (StringUtils.isEmpty(oldPassword)||StringUtils.isEmpty(newPassword)){
            jsonResult.setMsg("密码不能为空");
            jsonResult.setSuccess(false);
            return jsonResult;
        }
        //从作用域中拿到当前员工对象
        Employee employee = UserContext.getCurrentUser();
        //对比用户输入的原密码是否正确
        if (employee.getPassword().equals(oldPassword)){
            //正确,进行修改密码的操作
            employeeService.updatePwd(employee.getId(),newPassword);
            //删除作用域
            UserContext.getSession().invalidate();
            return jsonResult;
        }else {
            //不正确,提示用户老密码输入错误
            jsonResult.setSuccess(false);
            jsonResult.setMsg("旧密码输入错误");
            return jsonResult;
        }
    }
    @RequiredPermission(name = "密码重置",expression = "permission:repsw")
    @RequestMapping("/restPws")
    public String rest(Long id,Model model){
        //拿到id查询出数据
        Employee employee = employeeService.get(id);
        System.out.println(employee);
        model.addAttribute("employee", employee);
        model.addAttribute("aaa", 222);
        //转到页面
        return "common/resetPwd";
    }

    @RequestMapping("/change")

    @ResponseBody
    public JsonResult rest(String newPassword,Long id){
        JsonResult jsonResult = new JsonResult();
        if (StringUtils.isEmpty(newPassword)){
            jsonResult.setMsg("密码不能为空");
            jsonResult.setSuccess(false);
        }
        //重置密码
        employeeService.updatePwd(id, newPassword);
        return jsonResult;

    }

}
